package ltd.dujiabao.capability.domain.capmng.sync;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import ltd.dujiabao.capability.common.domain.AggregateRoot;
import ltd.dujiabao.capability.domain.capmng.cap.Capability;
import ltd.dujiabao.capability.domain.devmng.DevInfo;
import ltd.dujiabao.capability.domain.devmng.DevType;

/**
 * @author : DuJiabao
 * @Project : ddd-capability
 * @Time : 2024/4/6 21:23
 * @Desc :
 */
public class Synchronizer extends AggregateRoot {
    private final DevType devType;
    private final List<Worker> workers;
    private final Map<String, Object> attributes;

    public Synchronizer(LocalDateTime createdAt, Long createdBy, DevType devType,
                        List<Worker> workers, Map<String, Object> attributes) {
        super(createdAt, createdBy);
        this.devType = devType;
        this.workers = workers;
        this.attributes = attributes;
    }

    public Capability syncCapability(DevInfo devInfo) {
        Capability capability = new Capability(LocalDateTime.now(), 0L,
                devInfo.getDevModelId(), devInfo.getHardwareVer(), devInfo.getFirmwareVer());
        SyncCapabilityContext context = new SyncCapabilityContext(capability, devInfo, attributes);
        for (Worker worker : workers) {
            if (worker.checkIfNeeded(context)) {
                worker.syncCapability(context);
            }
        }
        return capability;
    }

    public DevType getDevType() {
        return devType;
    }
}
